# -*- coding: utf-8 -*-
from flask import json
import redis
from scrapy_redis.spiders import RedisSpider
from scrapy import Request, FormRequest
from spider.ops import Ops
import subprocess

from facility.reg_fn import get_number


class HuienSpider(RedisSpider):
    name = 'huien'
    redis_key = 'huien:start_urls'
    v8host = 'http://localhost:5000/'

    con = redis.Redis(host='localhost', port=6379, db=0)
    con.delete(redis_key)
    con.lpush(redis_key, v8host)

    def parse(self, response):
        yield FormRequest(url='https://www.huienziben.com/registration/user_login',
                          formdata={"userName": "18215629392", "pwd": "xx805827"},
                          headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                                   "X-Requested-With": "XMLHttpRequest",
                                   "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"},
                          dont_filter=True, callback=self.parse_item)

        # yield FormRequest(
        #    url = self.v8host,
        #    formdata = {"to" : "https://www.klmygsjr.com/BID/Debt/DebtList"},
        #    dont_filter=True,
        #    callback = self.parse_item
        # )

    def parse_item(self, response):
        q = Ops(response)
        for i in range(397):
            yield FormRequest(url='https://www.huienziben.com/loaninfo/loanList.htm',
                              formdata={'money': '0', 'month': '0', 'type': '0', 'rank': '0', 'loanType': '0',
                                        'no': '{}'.format(i + 1)},
                              dont_filter=True, callback=self.parse_detail)

    def parse_end(self, response):
        q = Ops(response)
        num = int(q.get_num('/html/body/table/tbody/tr'))

        for i in range(num):
            data = ""
            try:
                data += q.get_str('/html/body/table/tbody/tr[{}]/div/div[1]/a'.format(i + 1))
                data += " "
                data += q.get_str('/html/body/table/tbody/tr[{}]/div/div[4]'.format(i + 1))
                data += " "
                data += q.get_str('/html/body/table/tbody/tr[{}]/div/div[2]'.format(i + 1))
                data += " "
                data += q.get_str('/html/body/table/tbody/tr[{}]/div/div[3]'.format(i + 1))
            except Exception as e:
                pass
            subprocess.call("echo  {} >>惠恩.txt".format(data.replace('￥', '').replace('元', '')), shell=True)

    def parse_detail(self, response):
        q = Ops(response)
        num = int(q.get_num('/html/body/table/tbody/tr'))

        for i in range(num):
            data = q.get_attr('/html/body/table/tbody/tr[{}]/div/div[1]/a'.format(i + 1), 'href')
            print('https://www.huienziben.com/{}'.format(data))
            yield Request(url='https://www.huienziben.com/{}'.format(data), callback=self.parse_detail_end)

    def parse_detail_end(self, response):
        q = Ops(response)
        name = q.get_str("/html/body/div[5]/div/div[2]/div/div[1]/div/h2")
        amount = q.get_str("/html/body/div[5]/div/div[2]/div/div[2]/dl[1]/dd/em")
        rate = str(q.get_str("/html/body/div[5]/div/div[2]/div/div[2]/dl[2]/dd/em")) + "%"

        try:
            ids = q.get_str("/html/body/div[5]/div/div[2]/div/div[1]/div/span")
            if ids is None or ids == []:
                start = name
            else:
                start = [i for i in get_number(ids) if len(str(i)) > 6][-1]
                start = start[0:4] + "-" + start[4:6] + "-" + start[6:8]
                if not isinstance(start, str):
                    print('error')

        except:
            start = name

        cycle = str(q.get_str("/html/body/div[5]/div/div[2]/div/div[2]/dl[3]/dd/em/em"))
        way = q.get_str("/html/body/div[5]/div/div[2]/div/div[3]/p[1]/span[2]/if")

        data = str(name) + " " + str(amount) + " " + str(rate) + " " + str(start) + " " + str(cycle) + " " + str(
            way) + " " + ids

        subprocess.call("echo  {} >>惠恩.txt".format(data), shell=True)
